﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_PGJGControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        DropDownList dllsqzt;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.RowDataBound += new GridViewRowEventHandler(CustomGridList1_RowDataBound);
            Bind();
            if (Page.Request["pnid"]!=null)
            {
                UpPG();
            }
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "') and rolename like '%市征收%' ";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                CustomGridList1.Columns[13].Visible = false;//申请抽签
                CustomGridList1.Columns[14].Visible = false;//申请发布
            }

            string sql1 = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "') and rolename like '%区征收%' ";
            DataTable dt1 = _EluZSDataProvider.GetTable(sql1);
            if(dt1.Rows.Count>0)
            {
                CustomGridList1.Columns[10].Visible = false;//申请抽签状态
                CustomGridList1.Columns[9].Visible = false;//申请发布状态
                //CustomGridList1.Columns[12].Visible = false;//选择中标机构
            }

            if (Page.Request["pnid"]!=null)
            {

                if (Page.Request["type"]!=null)
                {
                    if (Page.Request["type"]  == "sqfb")
                    {
                        string sql4 = "update zs_project set 部门申请状态 = '1' where id = '" + Page.Request["pnid"] + "'";
                        _EluZSDataProvider.GetTable(sql4);
                    }
                    else if (Page.Request["type"] == "ysq")
                    {
                        string sql2 = "update zs_project set 是否抽签 = '0' where id = '" + Page.Request["pnid"] + "' ";
                        _EluZSDataProvider.GetTable(sql2);
                    }
                   
                }
                else
                {
                    string sql2 = "update zs_project set 是否抽签 = '1' where id = '" + Page.Request["pnid"] + "' ";
                    _EluZSDataProvider.GetTable(sql2);
                }
               
            }
           
        }

     
       
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            Button btnsearch = (Button)FindControl("btnSeach");
            btnsearch.Click += new EventHandler(Go_Search);
            DataTable dt = GetProUnList1("",GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            
        }

        public DataTable GetProUnList1(string column, string sqlwhere)
        {
            string sql = string.Format(@"Select t.Id,
                           t.立项号,
                           t.项目名称,
                           t.建设单位,
                           t.征收范围,
                           (Select 征收许可证号 From Zs_Prono p Where p.Pid = t.Id and rownum =1 ) 征收决定号,
                           (Select 已通过征收评估机构 From Zs_Prono p Where p.Pid = t.Id and rownum =1 ) 征收评估机构,
                           (Case When 部门申请状态 = '0' Then '未申请' Else '已申请' End ) 申请状态,
                           (Case When 是否抽签  = '0' Then '未申请' Else '已申请' End ) 抽签状态, t.Id Pid {0}
                      From Zs_Project t
                     Where 1 = 1
                    ", column);
            if (!string.IsNullOrEmpty(sqlwhere)) { sql += sqlwhere; }
            DataTable dt = _EluZSDataProvider.GetTable(sql);      
            return dt;
        }

       
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            TextBox txtProName = (TextBox)FindControl("txtProName");
            TextBox txtProNo = (TextBox)FindControl("txtProNo");
            TextBox txtComm = (TextBox)FindControl("txtComm");
            TextBox txtzspg = (TextBox)FindControl("txtzspg");
            DropDownList dllsqzt = (DropDownList)FindControl("dllsqzt");
            DropDownList dllsqcq = (DropDownList)FindControl("dllsqcq");
            string proName = txtProName.Text;
            string proNo = txtProNo.Text;
            string jsComm = txtComm.Text;
            string zspg = txtzspg.Text;
            if (!string.IsNullOrEmpty(zspg))
            {
                sqlWhere += " and t.Id In (Select pid From zs_prono Where 已通过征收评估机构 Like '%" + zspg + "%' )";
            }

            if (!string.IsNullOrEmpty(proName))
            {
                sqlWhere += " and t.项目名称 like '%" + proName + "%'";
            }
            if (!string.IsNullOrEmpty(proNo))
            {
                sqlWhere += " and t.立项号 like '%" + proNo + "%'";
            }
            if (!string.IsNullOrEmpty(jsComm))
            {
                sqlWhere += " and t.建设单位 like '%" + jsComm + "%'";
            }
            string sqzt = dllsqzt.SelectedValue;
            if (!string.IsNullOrEmpty(sqzt))
            {
                if (sqzt == "未申请")
                {
                    sqlWhere += "And t.部门申请状态 = '0'";
                }
                if (sqzt == "已申请")
                {
                    sqlWhere += "And t.部门申请状态 = '1'";
                }
              
            }

            string sqcq = dllsqcq.SelectedValue;
            if (!string.IsNullOrEmpty(sqcq))
            {
                if (sqcq == "未申请")
                {
                    sqlWhere += " And t.是否抽签 = '0'";
                }
                if (sqcq == "已申请")
                {
                    sqlWhere += "And t.是否抽签 = '1'";
                }
            }

            if (_EluZSDataProvider.CkComType(userid) == "评估机构")
            { }
            else
            {
                if (_EluZSDataProvider.IsQZSBM(userid) && !_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
                {
                    sqlWhere += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||t.区属||'%' ";
                }
            }
            return sqlWhere;
        }



        protected void CustomGridList1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string id = rowview["Id"].ToString();
                string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "') and (rolename like '%区征收部门%' or rolename like '%超级管理员%') ";
                 DataTable dt = _EluZSDataProvider.GetTable(sql);
                 if (dt.Rows.Count > 0)
                 {
                     string sql1 = "Select 部门申请状态 from zs_project where id='" + id + "' ";
                     DataTable dt1 = _EluZSDataProvider.GetTable(sql1);
                     if (dt1.Rows.Count > 0)
                     {
                         if (dt1.Rows[0]["部门申请状态"].ToString().Equals("1"))
                         {
                             e.Row.Cells[14].Text = "已申请";
                             e.Row.Cells[14].Attributes.Add("style", "cursor:pointer;color:#23388;");
                             e.Row.Cells[14].Attributes.Add("href", "javascript:void(0)");
                             e.Row.Cells[14].Attributes.Add("onclick", "javascript:alert('已经申请！');");
                         }
                         if (dt1.Rows[0]["部门申请状态"].ToString().Equals("0"))
                         {
                             e.Row.Cells[14].Text = "申请发布";
                             e.Row.Cells[14].Attributes.Add("style", "cursor:pointer;color:#223399;");
                             e.Row.Cells[14].Attributes.Add("href", "javascript:void(0)");
                             e.Row.Cells[14].Attributes.Add("onclick", "javascript:window.location.href=('ZS_PGJG.aspx?PNID=" + id + "&type=sqfb');");
                         }
                         string sql2 = "Select 是否抽签 from zs_project where id='" + id + "' ";
                         DataTable dt2 = _EluZSDataProvider.GetTable(sql2);
                         if (dt2.Rows.Count > 0)
                         {
                             if (dt2.Rows[0]["是否抽签"].ToString().Equals("1"))
                             {
                                 e.Row.Cells[13].Text = "已申请";
                                 e.Row.Cells[13].Attributes.Add("style", "cursor:pointer;color:#23388;");
                                 e.Row.Cells[13].Attributes.Add("href", "javascript:void(0)");
                                 e.Row.Cells[13].Attributes.Add("onclick", "javascript:window.location.href=('ZS_PGJG.aspx?PNID=" + id + "&type=ysq');");
                             }
                             if (dt2.Rows[0]["是否抽签"].ToString().Equals("0"))
                             {
                                 e.Row.Cells[13].Text = "申请抽签";
                                 e.Row.Cells[13].Attributes.Add("style", "cursor:pointer;color:#223399;");
                                 e.Row.Cells[13].Attributes.Add("href", "javascript:void(0)");
                                 e.Row.Cells[13].Attributes.Add("onclick", "javascript:window.location.href=('ZS_PGJG.aspx?PNID=" + id + "');");
                             }
                         }
                     }

                     
                 }
                 else
                 {
                     //string sql1 = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "') and rolename like '%市征收办%' ";
                     //DataTable dt1 = _EluZSDataProvider.GetTable(sql1);
                     //if (dt1.Rows.Count>0)
                     //{
                     //    string sql2 = "Select 部门申请状态 from zs_project where id='" + id + "' ";
                     //    DataTable dt2 = _EluZSDataProvider.GetTable(sql2);
                     //    if (dt2.Rows.Count > 0)
                     //    {
                     //        if (dt2.Rows[0]["部门申请状态"].ToString().Equals("1"))
                     //        {
                     //            e.Row.Cells[10].Text = "已申请";
                     //            e.Row.Cells[10].Attributes.Add("style", "cursor:pointer;color:#223399;");
                     //            e.Row.Cells[10].Attributes.Add("href", "javascript:void(0)");
                     //            e.Row.Cells[10].Attributes.Add("onclick", "javascript:if(confirm('区县已经提交申请，是否进行信息发布?')){ location.href = ('ZS_PGFBSQ.aspx?PNID="+id+"');}");
                     //        }
                     //        if (dt2.Rows[0]["部门申请状态"].ToString().Equals("0"))
                     //        {
                     //            e.Row.Cells[10].Text = "未申请";
                     //            e.Row.Cells[10].Attributes.Add("style", "cursor:pointer;color:#223399;");
                     //            e.Row.Cells[10].Attributes.Add("href", "javascript:void(0)");
                     //            e.Row.Cells[10].Attributes.Add("onclick", "javascript:alert('该单位还未申请!')");
                     //        }
                     //    }
                     //}

                     //string sql3 = "Select 是否抽签 from zs_project where id='" + id + "' ";
                     //DataTable dt3 = _EluZSDataProvider.GetTable(sql3);
                     //if (dt3.Rows.Count > 0)
                     //{
                     //    if (dt3.Rows[0]["是否抽签"].ToString().Equals("1"))
                     //    {
                     //        e.Row.Cells[13].Text = "已申请";
                     //        e.Row.Cells[13].Attributes.Add("style", "cursor:pointer;color:#23388;");
                     //        e.Row.Cells[13].Attributes.Add("href", "javascript:void(0)");
                     //        e.Row.Cells[13].Attributes.Add("onclick", "javascript:window.location.href=('ZS_PGJG.aspx?PNID=" + id + "&type=ysq');");
                     //    }
                     //    if (dt3.Rows[0]["是否抽签"].ToString().Equals("0"))
                     //    {
                     //        e.Row.Cells[13].Text = "申请";
                     //        e.Row.Cells[13].Attributes.Add("style", "cursor:pointer;color:#223399;");
                     //        e.Row.Cells[13].Attributes.Add("href", "javascript:void(0)");
                     //        e.Row.Cells[13].Attributes.Add("onclick", "javascript:window.location.href=('ZS_PGJG.aspx?PNID=" + id + "');");
                     //    }
                     //}
                 }
            }
        }


        protected void UpPG()
        {
            string sqlusername = "Select name from zs_user where id = '" + userid + "'";
            DataTable dt = _EluZSDataProvider.GetTable(sqlusername);
            if (dt.Rows.Count>0)
            {
                string sql = "update zs_prono set 申请状态 = 1,申请人 ='" + dt.Rows[0]["name"] + "',申请时间=sysdate  where id = '" + Page.Request["PNID"] + "' ";
                _EluZSDataProvider.RunSqlNon(sql);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('申请成功！');location.href='zs_pgjg.aspx';</script>");
                
            }
        }

    }
}


